$(function () {
    var token = getCookie("token-user");
    var url = "http://localhost:8082/crm/back/dict/findByPage";
    $.ajax({
        url:url,
        headers:{
            "token":token
        },
        type:"POST",
        dataType: 'json',
        success:function(ret){
            console.log(ret);
            var str = "";
            $.each(ret.data.rows,function (i,dict) {
                str += "<tr><td>"+dict.dictId+"</td>\n" +
                    "\t\t\t\t\t\t\t\t<td class=\"center\">"+dict.dictType+"</td>\n" +
                    "\t\t\t\t\t\t\t\t<td class=\"center\">"+dict.dictItem+"</td>\n" +
                    "\t\t\t\t\t\t\t\t<td class=\"center\">\n" +dict.dictValue+
                    "\t\t\t\t\t\t\t\t</td>\n" +
                    "\t\t\t\t\t\t\t\t<td class=\"center\">\n" +dict.isEditable+
                    "\t\t\t\t\t\t\t\t</td>\n" +
                    "\t\t\t\t\t\t\t\t<td class=\"center\">\n" +
                    "\t\t\t\t\t\t\t\t\t<a class=\"btn btn-success\" value='"+dict.dictId+"' href=\"javascript:void(0);\">\n" +
                    "\t\t\t\t\t\t\t\t\t\t<i class=\"icon-zoom-in icon-white\"></i>  \n" +
                    "\t\t\t\t\t\t\t\t\t\t查看\n" +
                    "\t\t\t\t\t\t\t\t\t</a>\n" +
                    "\t\t\t\t\t\t\t\t\t<a class=\"btn btn-info\" value='"+dict.dictId+"' href=\"javascript:void(0);\">\n" +
                    "\t\t\t\t\t\t\t\t\t\t<i class=\"icon-edit icon-white\"></i>  \n" +
                    "\t\t\t\t\t\t\t\t\t\t编辑\n" +
                    "\t\t\t\t\t\t\t\t\t</a>\n" +
                    "\t\t\t\t\t\t\t\t\t<a class=\"btn btn-danger\" value='"+dict.dictId+"' href=\"javascript:void(0);\">\n" +
                    "\t\t\t\t\t\t\t\t\t\t<i class=\"icon-trash icon-white\"></i> \n" +
                    "\t\t\t\t\t\t\t\t\t\t删除\n" +
                    "\t\t\t\t\t\t\t\t\t</a>\n" +
                    "\t\t\t\t\t\t\t\t</td>\n" +
                    "\t\t\t\t\t\t\t</tr>"
            });
            $("#dictTable").append(str);
            $("#curPage").html(ret.data.curPage);
            $("#pageCount").html(ret.data.pageCount);
        }
    });

    $("#prePage").click(function () {
        var pageNo = parseInt($("#curPage").html())-1;
        if(pageNo<1){
            alert("已经是第一页了!");
        }else{
            var dictType = $("#dictType").val();
            var dictItem = $("#dictItem").val();
            var dictValue = $("#dictValue").val();
            showPage(pageNo,dictType,dictItem,dictValue);
            //showPage(pageNo);
        }
    });
    $("#nextPage").click(function () {
        var pageNo = parseInt($("#curPage").html())+1;
        var pageCount = parseInt($("#pageCount").html());
        if(pageNo>pageCount){
            alert("已经是最后一页了!");
        }else{
            var dictType = $("#dictType").val();
            var dictItem = $("#dictItem").val();
            var dictValue = $("#dictValue").val();
            showPage(pageNo,dictType,dictItem,dictValue);
            //showPage(pageNo);
        }
    });
    //查看
    $("#dictTable").on("click","a[class=\"btn btn-success\"]",function(){
        var dictId = $(this).attr("value");
        alert("view-btn-success-"+dictId);
    });
    //修改
    $("#dictTable").on("click","a[class=\"btn btn-info\"]",function(){
        var dictId = $(this).attr("value");
        alert("view-btn-info-"+dictId);
    });
    //删除
    $("#dictTable").on("click","a[class=\"btn btn-danger\"]",function(){
        var dictId = $(this).attr("value");
        alert("view-btn-danger-"+dictId);
    });

    //多条件查询
    $("#btnSearch").click(function () {
        var dictType = $("#dictType").val();
        var dictItem = $("#dictItem").val();
        var dictValue = $("#dictValue").val();
        showPage(1,dictType,dictItem,dictValue);
    });


    function showPage(pageNo,dictType,dictItem,dictValue) {
    //var data = {"pageNo":pageNo,"dictType":"客","pageSize":2};
    var data = {"pageNo":pageNo,"dictType":dictType,"dictItem":dictItem,"dictValue":dictValue};
    $.ajax({
        url:url,
        headers:{
            "token":token
        },
        contentType:"application/json",
        type:"POST",
        dataType:"json",
        data:JSON.stringify(data),
        success:function(ret){
            $("#dictTable").html("");
            var str = "";
            $.each(ret.data.rows,function (i,dict) {
                str += "<tr><td>"+dict.dictId+"</td>\n" +
                    "\t\t\t\t\t\t\t\t<td class=\"center\">"+dict.dictType+"</td>\n" +
                    "\t\t\t\t\t\t\t\t<td class=\"center\">"+dict.dictItem+"</td>\n" +
                    "\t\t\t\t\t\t\t\t<td class=\"center\">\n" +dict.dictValue+
                    "\t\t\t\t\t\t\t\t</td>\n" +
                    "\t\t\t\t\t\t\t\t<td class=\"center\">\n" +dict.isEditable+
                    "\t\t\t\t\t\t\t\t</td>\n" +
                    "\t\t\t\t\t\t\t\t<td class=\"center\">\n" +
                    "\t\t\t\t\t\t\t\t\t<a class=\"btn btn-success\" value='"+dict.dictId+"' href=\"javascript:void(0);\">\n" +
                    "\t\t\t\t\t\t\t\t\t\t<i class=\"icon-zoom-in icon-white\"></i>  \n" +
                    "\t\t\t\t\t\t\t\t\t\t查看\n" +
                    "\t\t\t\t\t\t\t\t\t</a>\n" +
                    "\t\t\t\t\t\t\t\t\t<a class=\"btn btn-info\" value='"+dict.dictId+"' href=\"javascript:void(0);\">\n" +
                    "\t\t\t\t\t\t\t\t\t\t<i class=\"icon-edit icon-white\"></i>  \n" +
                    "\t\t\t\t\t\t\t\t\t\t编辑\n" +
                    "\t\t\t\t\t\t\t\t\t</a>\n" +
                    "\t\t\t\t\t\t\t\t\t<a class=\"btn btn-danger\" value='"+dict.dictId+"' href=\"javascript:void(0);\">\n" +
                    "\t\t\t\t\t\t\t\t\t\t<i class=\"icon-trash icon-white\"></i> \n" +
                    "\t\t\t\t\t\t\t\t\t\t删除\n" +
                    "\t\t\t\t\t\t\t\t\t</a>\n" +
                    "\t\t\t\t\t\t\t\t</td>\n" +
                    "\t\t\t\t\t\t\t</tr>"
            });
            $("#dictTable").append(str);
            $("#curPage").html(ret.data.curPage);
            $("#pageCount").html(ret.data.pageCount);
        }
    });
}
});